//
//  WBStatusPictureView.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/6/6.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    
    var currentViewmodel:WBStatusViewModel?
    

    func seupViewModel(viewmodel:WBStatusViewModel?) {
        
        currentViewmodel = viewmodel
        
        calcView()
    }
    
    //根据配图大小显示内容
    func calcView() {
        
        //处理宽度
        //单张图－图片大小，修改宽度
        if currentViewmodel?.picURLS?.count == 1 {
            
            let viewSize = currentViewmodel?.pictureViewSize ?? CGSize()
        
            let v = subviews[0]
            v.frame = CGRect(x: 0.0, y: CGFloat(WBStatusPictureViewOutMargin), width: viewSize.width, height: viewSize.height - CGFloat(WBStatusPictureViewOutMargin))
        }
        else{//多图张图－保证9宫格布局
            
            let v = subviews[0]
            v.frame = CGRect(x: 0.0, y: CGFloat(WBStatusPictureViewOutMargin), width: WBStatusPictureViewItemWidth, height: WBStatusPictureViewItemWidth)
            
        
        }
        
        //设置成模型中计算好的高度
        heightCons.constant = currentViewmodel?.pictureViewSize.height ?? 0
    }
    
    
    func setupUrls(urls:[WBStatusPicture]?) {
        
        for v in subviews {
            v.isHidden = true
        }
        
        var index = 0
        
        for url in urls ?? [] {
            
            let v = subviews[index] as! UIImageView
            
            //处理4张图片,跳过第3张，使用4，5
            if index == 1 && urls?.count == 4 {
                index += 1
            }
            
            v.wb_setimage(urlstring: url.thumbnail_pic, placeholderImage: nil)
            v.isHidden = false
            index += 1
        }
        
        
    }


    @IBOutlet weak var heightCons:NSLayoutConstraint!
    
    override func awakeFromNib() {
        
        setupUI()
    }
    

}

extension WBStatusPictureView {

    //提前准备控件
    fileprivate func setupUI(){
        
        backgroundColor = superview?.backgroundColor
        
        clipsToBounds = true
    
        //循环创建9个空间
        let count = 3
        let rect = CGRect(x: CGFloat(0.0),
                          y: CGFloat(WBStatusPictureViewOutMargin),
                          width: WBStatusPictureViewItemWidth,
                          height: WBStatusPictureViewItemWidth)
        
        for i in 0..<count * count{
            
            let v = UIImageView()
            
            v.backgroundColor = UIColor.red
            
            v.contentMode = .scaleAspectFill
            v.clipsToBounds = true
            
            //行的位置
            let row = CGFloat(i / count)
            //列 的位置
            let col =  CGFloat(i % count)
            
            let xOffset =  col * (CGFloat(WBStatusPictureViewItemWidth) + CGFloat(WBStatusPictureViewInnerMargin))
            let yOffset =  row * (CGFloat(WBStatusPictureViewItemWidth) + CGFloat(WBStatusPictureViewInnerMargin))

            v.frame = rect.offsetBy(dx:xOffset, dy:yOffset)
            
            addSubview(v)
        }
    }
}
